﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework;
using Terraria.ModLoader;

namespace Terraria.GameContent
{
	// Token: 0x020003BF RID: 959
	public class TownRoomManager
	{
		// Token: 0x0600220C RID: 8716 RVA: 0x00017ECE File Offset: 0x000160CE
		public int FindOccupation(int x, int y)
		{
			return this.FindOccupation(new Point(x, y));
		}

		// Token: 0x0600220D RID: 8717 RVA: 0x0046AC60 File Offset: 0x00468E60
		public int FindOccupation(Point tilePosition)
		{
			foreach (Tuple<int, Point> tuple in this._roomLocationPairs)
			{
				if (tuple.Item2 == tilePosition)
				{
					return tuple.Item1;
				}
			}
			return -1;
		}

		// Token: 0x0600220E RID: 8718 RVA: 0x00017EDD File Offset: 0x000160DD
		public bool HasRoomQuick(int npcID)
		{
			return this._hasRoom[npcID];
		}

		// Token: 0x0600220F RID: 8719 RVA: 0x0046ACC8 File Offset: 0x00468EC8
		public bool HasRoom(int npcID, out Point roomPosition)
		{
			if (!this._hasRoom[npcID])
			{
				roomPosition = new Point(0, 0);
				return false;
			}
			foreach (Tuple<int, Point> tuple in this._roomLocationPairs)
			{
				if (tuple.Item1 == npcID)
				{
					roomPosition = tuple.Item2;
					return true;
				}
			}
			roomPosition = new Point(0, 0);
			return false;
		}

		// Token: 0x06002210 RID: 8720 RVA: 0x00017EE7 File Offset: 0x000160E7
		public void SetRoom(int npcID, int x, int y)
		{
			this._hasRoom[npcID] = true;
			this.SetRoom(npcID, new Point(x, y));
		}

		// Token: 0x06002211 RID: 8721 RVA: 0x0046AD58 File Offset: 0x00468F58
		public void SetRoom(int npcID, Point pt)
		{
			this._roomLocationPairs.RemoveAll((Tuple<int, Point> x) => x.Item1 == npcID);
			this._roomLocationPairs.Add(Tuple.Create<int, Point>(npcID, pt));
		}

		// Token: 0x06002212 RID: 8722 RVA: 0x00017F00 File Offset: 0x00016100
		public void KickOut(NPC n)
		{
			this.KickOut(n.type);
			this._hasRoom[n.type] = false;
		}

		// Token: 0x06002213 RID: 8723 RVA: 0x0046ADA4 File Offset: 0x00468FA4
		public void KickOut(int npcType)
		{
			this._roomLocationPairs.RemoveAll((Tuple<int, Point> x) => x.Item1 == npcType);
		}

		// Token: 0x06002214 RID: 8724 RVA: 0x0046ADD8 File Offset: 0x00468FD8
		public void DisplayRooms()
		{
			foreach (Tuple<int, Point> tuple in this._roomLocationPairs)
			{
				Dust.QuickDust(tuple.Item2, Main.hslToRgb((float)tuple.Item1 * 0.05f % 1f, 1f, 0.5f));
			}
		}

		// Token: 0x06002215 RID: 8725 RVA: 0x0046AE54 File Offset: 0x00469054
		public void Save(BinaryWriter writer)
		{
			int num = 0;
			foreach (Tuple<int, Point> tuple in this._roomLocationPairs)
			{
				if (tuple.Item1 < 580)
				{
					num++;
				}
			}
			writer.Write(num);
			foreach (Tuple<int, Point> tuple2 in this._roomLocationPairs)
			{
				if (tuple2.Item1 < 580)
				{
					writer.Write(tuple2.Item1);
					writer.Write(tuple2.Item2.X);
					writer.Write(tuple2.Item2.Y);
				}
			}
		}

		// Token: 0x06002216 RID: 8726 RVA: 0x0046AF38 File Offset: 0x00469138
		public void Load(BinaryReader reader)
		{
			this.Clear();
			int num = reader.ReadInt32();
			for (int i = 0; i < num; i++)
			{
				int num2 = reader.ReadInt32();
				Point item = new Point(reader.ReadInt32(), reader.ReadInt32());
				this._roomLocationPairs.Add(Tuple.Create<int, Point>(num2, item));
				this._hasRoom[num2] = true;
			}
		}

		// Token: 0x06002217 RID: 8727 RVA: 0x0046AF94 File Offset: 0x00469194
		public void Clear()
		{
			this._roomLocationPairs.Clear();
			for (int i = 0; i < this._hasRoom.Length; i++)
			{
				this._hasRoom[i] = false;
			}
		}

		// Token: 0x06002218 RID: 8728 RVA: 0x0046AFC8 File Offset: 0x004691C8
		public byte GetHouseholdStatus(NPC n)
		{
			byte result = 0;
			if (n.homeless)
			{
				result = 1;
			}
			else if (this.HasRoomQuick(n.type))
			{
				result = 2;
			}
			return result;
		}

		// Token: 0x04003E26 RID: 15910
		internal List<Tuple<int, Point>> _roomLocationPairs = new List<Tuple<int, Point>>();

		// Token: 0x04003E27 RID: 15911
		internal bool[] _hasRoom = new bool[NPCLoader.NPCCount];
	}
}
